package com.creaylei.stock.converter;

import com.creaylei.stock.dto.ExportStockDto;
import com.creaylei.stock.po.StockPo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author zhang lei
 * @Date 2022-03-13 20:44
 */
@Slf4j
public class StockPoConverter {
    /**
     * ExportStockDto转StockPo
     * @param exportStockDto
     * @return
     */
    public StockPo convertToPo(ExportStockDto exportStockDto){
        if(exportStockDto == null){
            log.warn("exportStockDto为空");
            return null;
        }

        StockPo stockPo = new StockPo();
        BeanUtils.copyProperties(exportStockDto, stockPo);
        return stockPo;
    }

    /**
     * StockPo转ExportStockDto
     * @param stockPo
     * @return
     */
    public ExportStockDto convertToDto(StockPo stockPo){
        if(stockPo == null){
            log.warn("stockPo为空");
            return null;
        }

        ExportStockDto exportStockDto = new ExportStockDto();
        BeanUtils.copyProperties(stockPo, exportStockDto);
        exportStockDto.setExist((double) Math.round(stockPo.getExist()));
        exportStockDto.setPrice(String.format("%.2f", Double.parseDouble(stockPo.getPrice())));
        exportStockDto.setMarketValue(String.format("%.2f", Double.parseDouble(stockPo.getMarketValue())));

        return exportStockDto;
    }

    /**
     * ExportStockDtoList转StockPoList
     * @param exportStockDtoList
     * @return
     */
    public List<StockPo> convertToPoList(List<ExportStockDto> exportStockDtoList){
        if(exportStockDtoList == null || exportStockDtoList.size() < 1){
            log.warn("exportStockDtoList为空");
            return null;
        }

        List<StockPo> stockPoList = new ArrayList<StockPo>();
        for(ExportStockDto bexportStockDto : exportStockDtoList){
            StockPo stockPo = convertToPo(bexportStockDto);
            stockPoList.add(stockPo);
        }

        return stockPoList;
    }

    /**
     * StockPoList转ExportStockDtoList
     * @param stockPoList
     * @return
     */
    public List<ExportStockDto> convertToDtoList(List<StockPo> stockPoList){
        if(stockPoList == null || stockPoList.size() < 1){
            log.warn("stockPoList为空");
            return null;
        }

        List<ExportStockDto> exportStockDtoList = new ArrayList<ExportStockDto>();
        for(StockPo stockPo : stockPoList){
            ExportStockDto exportStockDto = convertToDto(stockPo);
            exportStockDtoList.add(exportStockDto);
        }

        return exportStockDtoList;
    }
}
